package com.datatools.ui.dialog;

import javax.swing.*;
import javax.swing.text.DefaultCaret;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

public class MsgDialog extends JDialog implements Serializable {
    private static final long serialVersionUID=1L;

    private JButton okBtn = new JButton("关闭");
    private JPanel btnPanel = new JPanel();
    private JTextArea jTextArea = new JTextArea();
    private JScrollPane scrollPane = new JScrollPane(jTextArea);

    private void init(){
        this.setResizable(true);
        this.setSize(300,200);
        Dimension max = new Dimension();
        max.setSize(500,400);
        this.setMaximumSize(max);
        this.setLayout(new BorderLayout());
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        this.setAlwaysOnTop(true);
        //this.setAutoRequestFocus(true);

        this.setModal(true);
        this.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
        this.setModalityType(ModalityType.APPLICATION_MODAL);

        jTextArea.setBounds(0,0,300,200);

        jTextArea.setEditable(false);

        scrollPane.setBounds(jTextArea.getBounds());
        //垂直滚动条
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        //滚动条随文字刷新显示
        DefaultCaret caret = (DefaultCaret) jTextArea.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

        btnPanel.setLayout(new BorderLayout());
        btnPanel.add(okBtn,BorderLayout.EAST);
        this.add(btnPanel,BorderLayout.SOUTH);
        this.add(scrollPane,BorderLayout.CENTER);

        okBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MsgDialogClose();
            }
        });
    }

    //static语句在初始化时只会执行一次。
    private static MsgDialog dialog = new MsgDialog();;

    private MsgDialog(){
        init();
    }
    public static void show(String msg){

        dialog.getjTextArea().setText(msg);
        //居中
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        dialog.repaint();
    }

    public static void MsgDialogClose(){
        dialog.dispose();
    }


    public JButton getOkBtn() {
        return okBtn;
    }

    public void setOkBtn(JButton okBtn) {
        this.okBtn = okBtn;
    }

    public JPanel getBtnPanel() {
        return btnPanel;
    }

    public void setBtnPanel(JPanel btnPanel) {
        this.btnPanel = btnPanel;
    }

    public JTextArea getjTextArea() {
        return jTextArea;
    }

    public void setjTextArea(JTextArea jTextArea) {
        this.jTextArea = jTextArea;
    }

    public JScrollPane getScrollPane() {
        return scrollPane;
    }

    public void setScrollPane(JScrollPane scrollPane) {
        this.scrollPane = scrollPane;
    }

    public static MsgDialog getDialog() {
        return dialog;
    }

    public static void setDialog(MsgDialog dialog) {
        MsgDialog.dialog = dialog;
    }
}
